package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.OrderUtil;
import com.windfone.model.Member;
import com.windfone.model.Order;
import com.windfone.model.ReceiveMsg;
import com.windfone.model.UserSendAddress;
import com.windfone.service.MemberService;
import com.windfone.service.OrderService;

public class OrderShowAction extends ActionSupport {
	/**	 */
	private static final long serialVersionUID = 1L;
	private OrderService orderService;
	private MemberService memberService;
	private List<Order> orderList ;
	private String rbtnPayType;
	private String rbtnPost;
	private String memo;
	private String detailaddress;
	private String telnumber;
	private String zipcode;
	private String email;
	private String mobile;
	private String name;
	private long orderHiddenSession;
	private String orderSeq;
	private ReceiveMsg receiveMsg;
	public ReceiveMsg getReceiveMsg() {
		return receiveMsg;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setReceiveMsg(ReceiveMsg receiveMsg) {
		this.receiveMsg = receiveMsg;
	}
	public String getOrderSeq() {
		return orderSeq;
	}
	public void setOrderSeq(String orderSeq) {
		this.orderSeq = orderSeq;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public String getRbtnPayType() {
		return rbtnPayType;
	}
	public String getRbtnPost() {
		return rbtnPost;
	}
	public String getMemo() {
		return memo;
	}
	public String getDetailaddress() {
		return detailaddress;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public String getTelnumber() {
		return telnumber;
	}
	public String getZipcode() {
		return zipcode;
	}
	public String getEmail() {
		return email;
	}
	public String getMobile() {
		return mobile;
	}
	public String getName() {
		return name;
	}
	public long getOrderHiddenSession() {
		return orderHiddenSession;
	}
	public void setRbtnPayType(String rbtnPayType) {
		this.rbtnPayType = rbtnPayType;
	}
	public void setRbtnPost(String rbtnPost) {
		this.rbtnPost = rbtnPost;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	public void setDetailaddress(String detailaddress) {
		this.detailaddress = detailaddress;
	}
	public void setTelnumber(String telnumber) {
		this.telnumber = telnumber;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setOrderHiddenSession(long orderHiddenSession) {
		this.orderHiddenSession = orderHiddenSession;
	}
	
	@Override
	public String execute() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
		Object orderSessionObj = request.getSession().getAttribute("orderSession");
		Object userObj= request.getSession().getAttribute("userBean");
		if ( orderSessionObj !=null && userObj!=null  ){
			int memberId = ((Member)userObj).getId();
			
			UserSendAddress bean = new UserSendAddress();
			bean.setDetailAddress(detailaddress);
			bean.setMemberid(memberId);
			bean.setMobile(mobile);
			bean.setZipCode(zipcode);
			bean.setTelNumber(telnumber);	
			bean.setReceiveName(name);
			bean.setEmail(email);
			if ( memberService.countOneUserSendAddres( bean )<=5){
				memberService.addUserSendAddres(bean);
			}
			
			long orderSession = (Long)orderSessionObj;
			if ( orderHiddenSession == orderSession ){
				orderSeq = OrderUtil.getOrderSeqByDatabase();
				double totalCost=0;
				receiveMsg = new ReceiveMsg();
				receiveMsg.setDetailaddress(detailaddress);
				receiveMsg.setEmail(email);
//				receiveMsg.setMemberid(memberid);
				receiveMsg.setMobile(mobile);
				receiveMsg.setName(name);
				receiveMsg.setOrderSeq(orderSeq);
				receiveMsg.setTelnumber(telnumber);
				receiveMsg.setZipcode(zipcode);
				receiveMsg.setPayType(rbtnPayType);
				receiveMsg.setPostType(rbtnPost) ;
				receiveMsg.setMemberId(memberId);
				receiveMsg.setUserMemo(memo);
				receiveMsg.setEmail(email);
				receiveMsg.setStatus(Order.NEED_PAY);
				if ( rbtnPost.equals("1")){
					receiveMsg.setPostcost(40);
					totalCost = 40;
				}else
					receiveMsg.setPostcost(0);
				request.getSession().removeAttribute("orderSession" );
				Object sessionObj = request.getSession().getAttribute("windfoneSessioin");
				String goodsName = "";
				if ( sessionObj!=null){
					orderList =  (List)sessionObj;
					for(int i=0;i<orderList.size();i++){
						Order o = orderList.get(i);
						if (i==0)
							goodsName = o.getName();
						else
							goodsName=goodsName+"," +o.getName();
						o.setOrderSeq(orderSeq);
						o.setMemberId(memberId);
						totalCost = totalCost + o.getPrice()*o.getOrderNumber();
						o.setStatus(Order.NEED_PAY);
						orderService.insertOrder(o);
					}
					receiveMsg.setAllGoodsName(goodsName);
					receiveMsg.setTotalCost(totalCost);
					orderService.insertReceiveMsg( receiveMsg );
					request.getSession().removeAttribute("windfoneSessioin");
					request.getSession().removeAttribute("orderSession");
				}
			}
		}
		return SUCCESS;
	}

	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
}
